﻿using AutoMapper;
using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Infatructrue;
using B.S.Outsourcing.Read.Api.Application.Command.OutSourOrderModel;
using MediatR;
using System.Collections.Generic;

namespace B.S.Outsourcing.Read.Api.Application.CommandHandler.OutSourOrderModel
{
    public class QueryOutsourOrderIDCommandHandler : IRequestHandler<QueryOutsourOrderIDCommand, ApiPaging<OutsourcingOrderModel>>
    {
        private readonly IBaseRepository<OutsourcingOrderModel> baseRepository;
        private readonly IMapper mapper;
        private readonly DBHelper1 dbHelper;

        public QueryOutsourOrderIDCommandHandler(IBaseRepository<OutsourcingOrderModel> baseRepository, DBHelper1 dbHelper, IMapper mapper = null)
        {
            this.baseRepository = baseRepository;
            this.dbHelper = dbHelper;
            this.mapper = mapper;
        }

        public async Task<ApiPaging<OutsourcingOrderModel>> Handle(QueryOutsourOrderIDCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<OutsourcingOrderModel> result = new ApiPaging<OutsourcingOrderModel>();
            var sql = from a in baseRepository.GetAll()
                      where request.Id.Contains(a.Id)  
                      select new OutsourcingOrderModel
                      {
                          Id = a.Id,
                          DocumentCode = a.DocumentCode,
                          BusinessType = a.BusinessType,
                          PlanOrderNumber = a.PlanOrderNumber,
                          MaterialCode = a.MaterialCode,
                          Supplier = a.Supplier,
                          DocumentDate = a.DocumentDate,
                          Operator = a.Operator,
                          MaterialName = a.MaterialName,
                          EntrustedContract = a.EntrustedContract,
                          DocumentStatus = a.DocumentStatus,
                          Auditor = a.Auditor,
                          OrderDeliveryDate = a.OrderDeliveryDate,
                          SpecificationModel = a.SpecificationModel,
                          OutsourcingQuantity = a.OutsourcingQuantity,
                          BusinessStatus = a.BusinessStatus,
                          AuditDate = a.AuditDate,
                          RequirementDate = a.RequirementDate,
                          DrawingNumber = a.DrawingNumber,
                          ProcessingFee = a.ProcessingFee,
                          Remarks = a.Remarks,
                          CreatedDate = a.CreatedDate,
                          CreatedBy = a.CreatedBy,
                          ModifiedDate = a.ModifiedDate,
                          ModifiedBy = a.ModifiedBy,
                          IsDelete = a.IsDelete,
                          SalesOrderNumber=a.SalesOrderNumber
                      };
            result.Code = ApiEnums.成功;
            result.Message = "成功";
            result.list = sql.ToList();
            return await Task.FromResult(result);
            //var str = dbHelper.Scalar(sql);
            //if (str != null)
            //{
            //    result.Code = ApiEnums.成功;
            //    result.Message = "成功";
            //    result.list=mapper.Map<ApiPaging<OutsourcingOrderModel>>(sql);
            //}
        }
    }
}
